"""Setup the blog application"""
import logging

from blog.config.environment import load_environment
from blog.model import meta
from blog import model
import time

log = logging.getLogger(__name__)

def setup_app(command, conf, vars):
    """Place any commands to setup blog here"""
    load_environment(conf.global_conf, conf.local_conf)

    # Create the tables if they don't already exist
    meta.metadata.create_all(bind=meta.engine)

    log.info('creating SuperUser...')
    user=model.User(username='Vic',email='Victorlee129@gmail.com')
    user.set_profile(model.Profile(description='The Super Admin here, yours Vic',sign='Admin is super :)',aka='Vic the Geek'))
    user.set_password('vic129lizeyv')
    user.set_right(model.Right(name='Vic',manage=32767,read=32767,write=32767,secrect=32767,reply=32767,create=32767))
    log.info('creating Management Blogger')
    blog=model.Blogger('Management',info='Blogging Admin Management Blog',summary='Admin Only')
    user.add_blogger(blog)
    article=model.Article(user=user,title='Blogging Auto Test Thread',content='Auto Test at %s' % time.ctime())
    thread=model.Thread(user=user,mainArticle=article)
    thread.set_right(model.Right(name='admin_reply_only',reply=255))
    reply=model.Article(user=user,title=None,content='Test replying')
    thread.add_reply(reply)
    blog.add_thread(thread)
    meta.Session.add(user)
    meta.Session.add(blog)
    meta.Session.add(thread)
    meta.Session.commit()
    log.info('Create Done')

    log.info('creting Default Right')
    right=model.Right('default')
    meta.Session.add(right)
    meta.Session.commit()
    log.info('Done')
    
